﻿using System;
using System.Linq;
using System.Web.Mvc;
using STX.Admin.Models.BizQuotePriceTemplate;
using STX.Service;
using STX.Domain;

namespace STX.Admin.Controllers
{
    public class BizQuotePriceTemplateController : BasePages1Controller<BizQuotePriceTemplate, IndexModel, AddModel, EditModel, QueryModel>
    {
        public BizQuotePriceTemplateService BizQuotePriceTemplateService { get; set; }
        public BizSupplierService BizSupplierService { get; set; }
        public BizMaterialCategoryService BizMaterialCategoryService { get; set; }
        public BizQuotePriceService BizQuotePriceService { get; set; }
        public BizAreaService BizAreaService { get; set; }

        protected override void OnInit(out Func<BaseService<BizQuotePriceTemplate>> thiService, out Func<IndexModel> getIndexModelFunc, out Func<AddModel> getAddModelFunc, out Func<int, EditModel> getEditModelFunc)
        {
            thiService = () => BizQuotePriceTemplateService;

            getIndexModelFunc = () =>
            {
                var areas = BizAreaService.FindAll();
                var materialCategories = BizMaterialCategoryService.FindAll();
                return new IndexModel
                {
                    MaterialCategories = new SelectList(materialCategories, "Id", "DisplayName"),
                    Areas = new SelectList(areas,"Id","Name")
                };
            };

            getAddModelFunc = () =>
            {
                //var suppliers = BizSupplierService.FindAll();
                var areas = BizAreaService.FindAll();
                var materialCategories = BizMaterialCategoryService.FindAll();
                return new AddModel
                {
                    //Suppliers = new SelectList(suppliers,"Id","Name"),
                    Areas = new SelectList(areas, "Id", "Name"),
                    MaterialCategories = new SelectList(materialCategories, "Id", "DisplayName")
                };
                
            };

            getEditModelFunc = id =>
            {
                var model = BizQuotePriceTemplateService.FindModelById(id);
                var areas = BizAreaService.FindAll();
                if (model == null)
                {
                    throw new Exception("你要查找的数据可能已被删除");
                }
                //var suppliers = BizSupplierService.FindAll();
                var materialCategories = BizMaterialCategoryService.FindAll();
                return new EditModel
                {
                    Id = id,
                    //Suppliers = new SelectList(suppliers, "Id", "Name",model.SupplierId),
                    MaterialCategories = new SelectList(materialCategories, "Id", "DisplayName", model.MaterialCategoryId),
                    Areas = new SelectList(areas, "Id", "Name",model.AreaId),
                    BizQuotePrices = model.BizQuotePriceList,
                    Name = model.Name,
                    Unit = model.Unit,
                    Required = model.Required,
                    Code =model.Code,
                    //SupplierId = model.SupplierId,
                    Split = model.Split,
                    FinanceCostPrice = model.FinanceCostPrice,
                    ProjectCostPrice = model.ProjectCostPrice
                };
            };
        }

        protected override long AddHandler(AddModel addModel)
        {

            var model = new BizQuotePriceTemplate()
            {
                Name = addModel.Name,
                MaterialCategoryId = addModel.MaterialCategoryId,
                //SupplierId = addModel.SupplierId,
                AreaId = addModel.AreaId,
                Split = addModel.Split,
                Code = addModel.Code,
                BizQuotePriceList = addModel.BizQuotePrices,
                Unit = addModel.Unit,
                Required = addModel.Required,
                FinanceCostPrice = addModel.FinanceCostPrice,
                ProjectCostPrice = addModel.ProjectCostPrice
            };
            return BizQuotePriceTemplateService.Add(model);
        }

        protected override bool EditHandler(EditModel editModel)
        {
            var model = BizQuotePriceTemplateService.FindModelById(editModel.Id);
            if (model == null)
            {
                throw new Exception("你要查找的数据可能已被删除");
            }
            model.Name = editModel.Name;
            model.Unit = editModel.Unit;
            model.Code = editModel.Code;
            model.AreaId = editModel.AreaId;
            model.Required = editModel.Required;
            model.Split = editModel.Split;
            //model.SupplierId = editModel.SupplierId;            
            model.MaterialCategoryId = editModel.MaterialCategoryId;
            model.FinanceCostPrice = editModel.FinanceCostPrice;
            model.ProjectCostPrice = editModel.ProjectCostPrice;
            BizQuotePriceService.RemoveIds(model.BizQuotePriceList.Select(o => o.Id).ToArray());
            model.BizQuotePriceList = editModel.BizQuotePrices;
            BizQuotePriceTemplateService.Update(model);
            return true;
        }

        protected override IQueryable<BizQuotePriceTemplate> QueryHandler(QueryModel queryModel, IQueryable<BizQuotePriceTemplate> queryable)
        {
            if (queryModel.AreaId > 0)
                queryable = queryable.Where(o => o.AreaId == queryModel.AreaId);
            if (queryModel.MaterialCategoryId>0)
                queryable = queryable.Where(o => o.MaterialCategoryId == queryModel.MaterialCategoryId);
            if (!queryModel.Code.IsNullOrEmpty())
            {
                queryable = queryable.Where(o => o.Code == queryModel.Code);
            }
            if (!queryModel.Name.IsNullOrEmpty())
                queryable = queryable.Where(o => o.Name.Contains(queryModel.Name.Trim()));
            return queryable;
        }

        protected override IQueryable<BizQuotePriceTemplate> OrderByHandler(IQueryable<BizQuotePriceTemplate> queryable)
        {
            return queryable.OrderByDescending(o=>o.Code);
        }

        public JsonResult CheckIsExistCode(string code,int id=0)
        {
            var result = BizQuotePriceTemplateService.FindAll().Where(o => o.Code.Trim() == code.Trim());
            if (id > 0)
            {
                result = result.Where(o => o.Id != id);
            }
            if (result.Any())
            {
                return new JsonResult
                {
                    Data = new { success = true, msg = "此编号已经存在了" }
                };
            }
            return new JsonResult
            {
                Data = new { success = true, msg = "" }
            };
            
        }

        public ContentResult GetAreaById(int areaId)
        {
            var model = BizAreaService.FindModelById(areaId);
            if (model != null)
            {
                return Content(model.Name);
            }
            return Content("");
        }
        
    }
}
